using UnityEngine; using System.Collections; using System.IO; using System.Text; using UnityEditor; [System.Serializable] public class Table { public string name; public TextAsset csvFile; } public class TableLoader : MonoBehaviour { public Table[] tables; public void loadTables() { StringBuilder classBody = new StringBuilder(); foreach (Table table in tables) { currentName = table.name; classBody.Append(loadTable(table.csvFile)); } string scriptContent = addScriptHeader(classBody.ToString()); createScriptAsset("Assets/Generated/GameData.cs",scriptContent); } string loadTable(TextAsset csv) { string csvString = csv.text; string[] lines = csvString.Split("\n"[0]); // debug: /* foreach (string line in lines) { Debug.Log(line); } */ if (lines.Length == 0) return ""; string[,] grid = toGrid(lines); return scriptify(grid); } string [,] toGrid(string [] lines) { int fieldNum = lines[0].Split(","[0]).Length; // assumes equally sized rows int lineNum = lines.Length; if (lines[lines.Length - 1] == "") --lineNum; string[,] grid = new string[lineNum, fieldNum]; for (int i = 0; i < lineNum; ++i) { string[] row = lines[i].Split(","[0]); for (int j = 0; j < row.Length; ++j) { grid[i, j] = row[j]; grid[i, j] = grid[i, j].Trim(new char[] { '\n', '\r', '\t', ' '}); } } // debug: /* for (int i = 0; i < grid.GetLength(0); ++i) { StringBuilder line = new StringBuilder(); for (int j = 0; j < grid.GetLength(1); ++j) { line.Append(grid[i, j]); line.Append(" "); } Debug.Log(line); } */ return grid; } string scriptify(string [,] grid) { StringBuilder result = new StringBuilder(); result.Append("\tpublic static class "+currentName+" {\n"); string tabs = "\t\t"; float dummy = 0; string type = "float"; string nullvalue = "0"; if (grid.GetLength (0) == 2) { // no level-based data for this cvs for (int j = 0; j < grid.GetLength(1); ++j) { if (grid [0, j].Length == 0) continue; string variableName = grid [0, j]; string value = grid [1, j]; dummy = 0; type = "float"; nullvalue = "0"; if (!float.TryParse(grid[1,j],out dummy)) { value = "\"" + value + "\""; type = "string"; nullvalue = "\"\""; } else { value = "(float)" + value; } if (value.Length == 0) { value = nullvalue; } result.Append (tabs + "public static "+ type + " " + variableName + " = " + value + ";\n"); } } else { // rows correspond to levels string array_suffix = "_array"; for (int j = 0; j < grid.GetLength(1); ++j) { if (grid [0, j].Length == 0) continue; string variableName = grid [0, j]; type = "float"; nullvalue = "0"; for (int i = 1; i < grid.GetLength(0); ++i) { // note that i starts from 1 string field = grid[i,j]; if (field.Length > 0 && !float.TryParse(field,out dummy)) { type = "string"; nullvalue = "\"\""; break; } } result.Append(tabs + "public static " + type + " " + variableName + " {\n"); result.Append(tabs + "\t" + "get {\n"); result.Append(tabs + "\t\t" + "if (currentLevel >= 0 && currentLevel < " + variableName + array_suffix + ".Length)\n"); result.Append(tabs + "\t\t\t" + "return " + variableName + array_suffix + "[currentLevel];\n"); result.Append(tabs + "\t\t" + "return " + nullvalue + ";\n"); result.Append(tabs + "\t} // get" + "\n"); result.Append(tabs + "} // " + variableName +"\n"); result.Append(tabs + "private static " + type + "[] " + variableName + array_suffix + " = \n"); result.Append(tabs + "\t" + "{\n"); for (int i = 1; i < grid.GetLength(0); ++i) { // note that i starts from 1 string value = grid[i,j]; if (value.Length == 0) value = nullvalue; else if (type == "string") value = "\"" + value + "\""; else if (type == "float") value = "(float)" + value; result.Append(tabs + "\t\t" + value + ",\n"); } result.Append(tabs + "\t" + "}; // array " + variableName + array_suffix +"\n"); } } result.Append("\t}// class "+currentName+"\n"); return result.ToString(); } void createScriptAsset(string path, string content) { if (File.Exists(path) ) { File.Delete(path); } using (StreamWriter outFile = new StreamWriter(path)) { outFile.Write(content); } AssetDatabase.Refresh(); } string addScriptHeader(string classBody) { StringBuilder result = new StringBuilder(); result.Append("using UnityEngine;\n"); result.Append("\n"); result.Append("public class GameData {\n"); result.Append("\tprivate static int currentLevel = 0;\n"); result.Append("\tpublic static void setLevel(int l) { currentLevel = l; }\n"); result.Append("\tpublic static void nextLevel() {++currentLevel;}\n"); result.Append(classBody); result.Append("} // class GameData"); return result.ToString(); } protected string currentName; }